package com.sgb.merchant.view.fragment;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.link.entity.merchant.DynamicListEntity;
import com.sgb.link.entity.merchant.IndustryListEntity;
import com.sgb.link.router.PathMerchant;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantFragIndustryDynamicBinding;
import com.sgb.merchant.view.adapter.DynamicAdapter;
import com.sgb.merchant.viewmodel.IndustryViewModel;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseFragment;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/10/13
 * @Description: 产业园区详情动态列表
 */
public class IndustryDynamicFragment extends BaseFragment<IndustryViewModel, MerchantFragIndustryDynamicBinding> {

    DynamicAdapter mAdapter;
    List<DynamicListEntity> mList=new ArrayList<>();
    String industrialParkNo;

    public static IndustryDynamicFragment newInstance(String industrialParkNo){
        IndustryDynamicFragment mFragment=new IndustryDynamicFragment();
        Bundle bundle=new Bundle();
        bundle.putString("industrialParkNo",industrialParkNo);
        mFragment.setArguments(bundle);
        return mFragment;
    }


    @Override
    protected int layoutId() {
        return R.layout.merchant_frag_industry_dynamic;
    }

    @Override
    protected void createViewModel() {
        mViewModel=new ViewModelProvider(this).get(IndustryViewModel.class);
    }

    @Override
    protected void init() {
        industrialParkNo=getArguments().getString("industrialParkNo");

        mBinding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerDecoration.builder(getActivity())
                .color(getResources().getColor(R.color.color_f5f5f5))
                .size(1, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.recycler);

        mAdapter=new DynamicAdapter(mList);
        mAdapter.bindToRecyclerView(mBinding.recycler);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build(PathMerchant.DynamicDetailActivity).withSerializable(RouterKey.DATA,mList.get(position)).navigation();
            }
        });
        mBinding.recycler.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.merchant_item_empty_view);

        mBinding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                getData(true);
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                getData(false);
            }
        });
        mViewModel.ldDynamic.observe(getActivity(),this::syncData);
        getData(false);
    }
    public void getData(boolean isLoadMore){
        mViewModel.getDynamicList(industrialParkNo,isLoadMore);
    }

    public void refresh(){
        mBinding.refreshLayout.autoRefresh();
    }

    @Override
    public void onClick(View v) {

    }




    /**
     * 产业园区动态数据
     *
     * @param
     */
    public void syncData(ListResponse<List<DynamicListEntity>> entity) {
        mBinding.refreshLayout.finishRefresh();
        mBinding.refreshLayout.finishLoadMore();
        if (mViewModel.dynamicPage == 1) {
            mList.clear();
        }
        if (entity.getList() != null) {
            mList.addAll(entity.getList());
        }
        mAdapter.notifyDataSetChanged();
        //
        if (entity.getCurrentPage() < entity.getTotalPage()) {
            mBinding.refreshLayout.resetNoMoreData();
        } else {
            mBinding.refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

}
